package com.joolun.web.api.foruminfo.controller;

import com.joolun.common.annotation.Anonymous;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.core.page.TableDataInfo;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.web.api.foruminfo.domain.ForumInfo;
import com.joolun.web.api.foruminfo.service.IForumInfoService;
import com.joolun.weixin.utils.ThirdSessionHolder;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帖子信息Controller
 * 
 * @author joolun
 * @date 2025-04-12
 */
@RestController
@Anonymous
@RequestMapping("/foruminfo/foruminfo")
public class ForumInfoController extends BaseController
{
    @Autowired
    private IForumInfoService forumInfoService;

    /**
     * 查询帖子信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ForumInfo forumInfo)
    {
        startPage();
        List<ForumInfo> list = forumInfoService.selectForumInfoList(forumInfo);
        return getDataTable(list);
    }

    /**
     * 导出帖子信息列表
     */
    @Log(title = "帖子信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumInfo forumInfo)
    {
        List<ForumInfo> list = forumInfoService.selectForumInfoList(forumInfo);
        ExcelUtil<ForumInfo> util = new ExcelUtil<ForumInfo>(ForumInfo.class);
        util.exportExcel(response, list, "帖子信息数据");
    }

    /**
     * 获取帖子信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(forumInfoService.selectForumInfoById(id));
    }

    /**
     * 新增帖子信息
     */
    @Log(title = "帖子信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumInfo forumInfo)
    {
        logger.info("ThirdSessionHolder.getWxUserId:{}",ThirdSessionHolder.getWxUserId());
        forumInfo.setUserId(ThirdSessionHolder.getWxUserId());
        return toAjax(forumInfoService.insertForumInfo(forumInfo));
    }

    /**
     * 修改帖子信息
     */
    @Log(title = "帖子信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumInfo forumInfo)
    {
        return toAjax(forumInfoService.updateForumInfo(forumInfo));
    }

    /**
     * 删除帖子信息
     */
    @Log(title = "帖子信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumInfoService.deleteForumInfoByIds(ids));
    }
}
